﻿/*  This file is part of PostreSQL Utilities.

    This is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with PostreSQL Utilities.  If not, see <http://www.gnu.org/licenses/>.
  
    Author : Riaan van der Westhuizen (riaan@definitive-software.com)
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PostgreSQL
{
    /// <summary>
    /// createdb -- create a new PostgreSQL database
    /// http://www.postgresql.org/docs/9.3/static/app-createdb.html
    /// </summary>
    public class CreateUtility : Utilty
    {
        #region Members
        private string databaseName = String.Empty;
        private string databaseDescription = String.Empty;
        private string tablespace = String.Empty;
        private string encoding = "UTF8";
        private string template = String.Empty;
        #endregion

        #region Constructors
        /// <summary>
        /// Default Constructors
        /// </summary>
        public CreateUtility() : base() 
        {
            UtilityFile = "createdb.exe";
        }

        /// <summary>
        /// Constructors with Connection Info.
        /// </summary>
        /// <param name="host">Specifies the host name of the machine on which the server is running.</param>
        /// <param name="port">Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections.</param>
        /// <param name="database">Specifies the name of the database to connect to.</param>
        /// <param name="username">User name to connect as.</param>
        /// <param name="password">User password.</param>
        public CreateUtility(string host, int port, string database, string username, string password)
            : base(host, port, database, username, password) 
        {
            UtilityFile = "createdb.exe";
        }
        #endregion

        #region Options
        /// <summary>
        /// Specifies the name of the database to be created. 
        /// The name must be unique among all PostgreSQL databases in this cluster. 
        /// The default is to create a database with the same name as the current system user.
        /// dbname
        /// </summary>
        public string DatabaseName
        {
            get { return databaseName; }
            set { databaseName = value; }
        }

        /// <summary>
        /// Specifies a comment to be associated with the newly created database.
        /// description
        /// </summary>
        public string DatabaseDescription
        {
            get { return databaseDescription; }
            set { databaseDescription = value; }
        }

        /// <summary>
        /// Specifies the default tablespace for the database. (This name is processed as a double-quoted identifier.)
        /// -D tablespace
        /// --tablespace=tablespace
        /// </summary>
        public string Tablespace
        {
            get { return tablespace; }
            set { tablespace = value; }
        }

        /// <summary>
        /// Specifies the character encoding scheme to be used in this database. 
        /// -E encoding
        /// --encoding=encoding
        /// </summary>
        public string Encoding
        {
            get { return encoding; }
            set { encoding = value; }
        }

        /// <summary>
        /// Specifies the template database from which to build this database. (This name is processed as a double-quoted identifier.)
        /// -T template
        /// --template=template
        /// </summary>
        public string Template
        {
            get { return template; }
            set { template = value; }
        }
        #endregion

        #region Arguments
        /// <summary>
        /// createdb command-line arguments
        /// </summary>
        /// <returns></returns>
        public override string Arguments()
        {
            StringBuilder arguments = new StringBuilder();



            arguments.Append(" " + databaseName);

            if (databaseDescription != String.Empty)
                arguments.Append(" " + databaseDescription);

            return arguments.ToString();
        }
        #endregion
    }
}
